<template>
  <div class="container">
    <div class="searchbar" v-show="querybarVisible">
      <el-form :model="queryForm" ref="queryForm" inline>
        <el-form-item label="应用系统名称" prop="appname">
          <el-input
            v-model="queryForm.appname"
            placeholder="请输入应用系统名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="searchData"
            >搜索</el-button
          ></el-form-item
        >
        <el-form-item>
          <el-button type="warning" icon="el-icon-refresh" @click="resetData"
            >重置</el-button
          ></el-form-item
        >
      </el-form>
    </div>
    <div class="main">
      <div class="toolbar">
        <el-button-group>
          <el-button type="primary" icon="el-icon-plus" @click="createApp"
            >添加</el-button
          >
          <el-button type="danger" icon="el-icon-delete">删除</el-button>
          <el-button
            type="success"
            icon="el-icon-refresh"
            @click="refreshapps"
            >刷新</el-button
          >
        </el-button-group>
        <el-button
          type="primary"
          class="right-toolbar"
          plain
          icon="el-icon-search"
          @click="querybarVisible = !querybarVisible"
        ></el-button>
      </div>
      <el-table :data="appData" style="width: 100%" border>
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="appname" label="系统名称"> </el-table-column>
        <el-table-column prop="create_date" label="创建时间"> </el-table-column>
        <el-table-column prop="developer" label="开发人员"> </el-table-column>
        <el-table-column label="操作" min-width="160" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleAppEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="pageIndex"
          :page-size="pageSize"
          :total="totalCount"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
      <!-- dialog -- 添加/编辑用例 -->
      <el-dialog
        :title="appDialogOptions.title"
        :visible.sync="appDialogOptions.visible"
        width="30%"
        :close-on-click-modal="false"
        @close="appDialogOptions.visible = false"
      >
        <el-form
          :model="appForm"
          :rules="appRules"
          ref="appForm"
          size="mini"
          label-width="100px"
        >
          <el-form-item label="系统名称" prop="name">
            <el-input v-model="appForm.name"></el-input>
          </el-form-item>
          <el-form-item label="开发人员" prop="developer">
            <el-input v-model="appForm.developer"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="appDialogOptions.visible = false"
            >取 消</el-button
          >
          <el-button
            type="primary"
            @click="handleApp(appDialogOptions.appid)"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import service from "../../api/service.js";
export default {
  data() {
    return {
      // 搜索栏是否显示
      querybarVisible: true,
      // 搜索表单
      queryForm: {
        appname: null
      },
      pageIndex: 1,
      pageSize: 10,
      totalCount: 0,
      appData: [],
      appForm: {
        appid: 0,
        name: "",
        developer: ""
      },
      appRules: {
        name: {
          required: true,
          min: 2,
          message: "请输入应用系统名称",
          trigger: "blur"
        }
      },
      appDialogOptions: {
        appid: 0,
        visible: false,
        title: "新增应用系统"
      }
    };
  },
  mounted() {
    this.refreshapps();
  },
  methods: {
    handlePageChange() {
      this.pageIndex = val;
      this.refreshapps();
    },
    handleAppEdit(idx, row) {
      this.resetappForm();
      this.appDialogOptions = {
        appid: row.appid,
        visible: true,
        title: "编辑应用系统信息"
      };
      Object.assign(this.appForm, row);
    },
    resetAppForm() {
      this.appForm = {
        appid: 0,
        name: "",
        developer: ""
      };
      if (this.$refs.appForm !== undefined) {
        this.$refs.appForm.resetFields();
      }
    },
    createApp() {
      this.resetappForm();
      this.appDialogOptions = {
        appid: 0,
        visible: true,
        title: "添加应用系统信息"
      };
    },
    refreshapps() {
      let _this = this;
      this.loading = true;
      service.getAppsAndDevs(this.pageIndex, this.pageSize).then(resp => {
        if (resp) {
          _this.appData = resp.content;
          _this.totalCount = resp.totalElements;
        }
        _this.loading = false;
      });
    },
    handleapp(id) {
      let _this = this;
      this.$refs["appForm"].validate(valid => {
        if (valid) {
          // 添加项目
          if (id == 0) {
            service.createApp(_this.appForm).then(resp => {
              if (resp) {
                _this.$message({
                  message: "保存应用系统信息成功",
                  type: "success"
                });
                _this.appData.push(resp);
                _this.resetappForm();
              }
            });
          } else {
            // 编辑项目
            service.updateApp(_this.appForm).then(resp => {
              if (resp) {
                _this.$message({
                  message: "保存应用系统信息成功",
                  type: "success"
                });
                let row = _this.appData.find(c => c.appid == id);
                Object.assign(row, _this.appForm);
                _this.resetappForm();
              } else {
                _this.$message({
                  message: "保存应用系统信息异常",
                  type: "warning"
                });
              }
            });
          }
          _this.appDialogOptions.visible = false;
        } else {
          _this.$message.error("保存应用系统信息失败");
          return false;
        }
      });
    },
    searchData() {
      //this.querySqls();
    },
    resetData() {
      this.queryForm = {
        appname: null
      };
    }
  }
};
</script>

<style>
</style>